
package com.yks.hbo.amazon.controller;

import com.alibaba.fastjson.JSONObject;
import com.yks.hbo.amazon.service.IPlsAmazonAdAccTokenService;
import com.yks.hbo.amazon.util.AmazonAdvertisingApiConstant;
import com.yks.hbo.amazon.util.AmazonAdvertisingApiUtil;
import com.yks.hbo.amazon.util.PlsPropertiesUtil;
import com.yks.hbo.amazon.util.VoHelper;
import com.yks.hbo.amazon.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
* @Description: Amazon 广告授权 Controller
* @Author: gaoliangyou
* @Date: 2020/12/2 15:34
*/
@RestController
    @RequestMapping("/api/amazon_ad")
public class AmazonAdvertisingOauthController {
    @Autowired
    IPlsAmazonAdAccTokenService plsAmazonAdAccTokenService;
    private static final Logger LOGGER = LoggerFactory.getLogger(AmazonAdvertisingOauthController.class);
    @RequestMapping(value = "/advertising_oauth", method = RequestMethod.GET)
    public ResultVO advertisingOauth(@RequestParam("region") String region){
        LOGGER.info(region);
        ResultVO resultVO = plsAmazonAdAccTokenService.advertisingOauth(region);
        return resultVO;
    }
    /**
    * @Description: Amazon 广告授权回调
    * @Param: [code, region]
    * @Return: java.lang.String
    * @Author: gaoliangyou
    * @Date: 2020/12/2 15:36
    */
    @RequestMapping(value = "/oauth_return")
    public String oauthReturn(@RequestParam(value = "code",required = false) String code, @RequestParam("region") String region) {
        if(code == null){
            return "code is null";
        }
        try {
            String url = "";
            String adApiUrl = "";
            String redirectUri = "";
            switch (region) {
                case "NA":
                    url = AmazonAdvertisingApiConstant.NaUrlPathPrefix.NA_AUTH_TOKEN_URL;
                    adApiUrl = AmazonAdvertisingApiConstant.NaUrlPathPrefix.NA_AD_API_URL;
                    redirectUri = PlsPropertiesUtil.getProperty("amazon.advertising.redirect.uri.na");
                    break;
                case "EU":
                    url = AmazonAdvertisingApiConstant.EuUrlPathPrefix.EU_AUTH_TOKEN_URL;
                    adApiUrl = AmazonAdvertisingApiConstant.EuUrlPathPrefix.EU_AD_API_URL;
                    redirectUri = PlsPropertiesUtil.getProperty("amazon.advertising.redirect.uri.eu");
                    break;
                case "FE":
                    url = AmazonAdvertisingApiConstant.FeUrlPathPrefix.FE_AUTH_TOKEN_URL;
                    adApiUrl = AmazonAdvertisingApiConstant.FeUrlPathPrefix.FE_AD_API_URL;
                    redirectUri = PlsPropertiesUtil.getProperty("amazon.advertising.redirect.uri.fe");
                    break;
                default:
                    break;
            }
            LOGGER.info("code{},region:{}",code,region);
            if(StringUtils.isEmpty(code) || StringUtils.isEmpty(region) || StringUtils.isEmpty(url)) {
                LOGGER.error("Amazon 广告授权回调参数异常，code：{}, region：{}", code, region);
                return null;
            }
            //LOGGER.info("Amazon 广告授权回调成功, code：{}, region:{}, url:{}, ", code, region, url);
            String result = AmazonAdvertisingApiUtil.getAmazonAdToken(code, redirectUri, url);
            LOGGER.info("result:{}",result);
            JSONObject jsonObject = JSONObject.parseObject(result);
            String accessToken = jsonObject.getString("access_token");
            String refreshToken = jsonObject.getString("refresh_token");
            LOGGER.info("accessToken：{}，refreshToken:{}",accessToken,refreshToken);
        }catch (Exception e) {
            LOGGER.error("AmazonAdvertisingOauthController > oauthReturn()广告授权回调>>授权信息入库异常，code：{}", code, e);
            return JSONObject.toJSONString(VoHelper.getErrorResult("授权成功但入库失败:{}", e.getMessage()));
        }
        return JSONObject.toJSONString(VoHelper.getSuccessResult());
    }

}
